// pages/my/my.js
const CONFIG = require('../../config');
import {$await} from '../../utils/util';
import {myPageAPI} from '../../api/public/index'
import {playbackAPI} from '../../api/playback/index'
const img_url = 'https://images.weserv.nl/?url='
// const request = require("../../utils/request");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl : CONFIG.imgUrlAvatar,
    imgUrlImg : CONFIG.imgUrlImg,
    interval: 2000,
		duration: 500,
    currentIndex: 0,
    myList:[
      {title:'我的订单',h5_url:""},
      {title:'系统通知',h5_url:CONFIG.channelsH5Url + 'systemInform?chnnelId=5148324&live_type=1&Truechnnel_id=8836&period=2105101400'},
      {title:'关于听见',h5_url:CONFIG.channelsH5Url + 'aboutRadio?live_Type=1'},
      {title:'反馈帮助',h5_url:CONFIG.H5TestUrl + 'html/help_h5.html?channel_id=8836&uid=' + wx.getStorageSync('userInfo').user_id},
    ]
  },
  toWebView(e){
    let { url , type} = e.currentTarget.dataset,webURL = '';
    type == 2 ? webURL = url + '&number='+ this.data.userInfo.number +'&Truechnnel_id=8836&period=2105101400&isToken='+wx.getStorageSync('userInfo').ds_token+'&oId='+wx.getStorageSync('userInfo').openid+'&token='+wx.getStorageSync('userInfo').token+'&salt='+wx.getStorageSync('userInfo').salt : webURL = url + '&isToken='+wx.getStorageSync('userInfo').ds_token+'&oId='+wx.getStorageSync('userInfo').openid+'&token='+wx.getStorageSync('userInfo').token+'&salt='+wx.getStorageSync('userInfo').salt
    console.log(webURL)
    wx.navigateTo({
      url: "/pages/webview/webview?webURL=" + encodeURIComponent(webURL),
    })
  },
  async getData(){
    let number = wx.getStorageSync('userInfo').user_id
    let [err,res] = await $await(myPageAPI.myPage({number}))
    if(res){
      if(res.nickname.length > 8){
        res.nickname = res.nickname.slice(0,7)+'...'
      }
      this.setData({
        userInfo : res,
        ['myList[0].h5_url']:res.order_url
      })
    }
  },
    // 获取广告位
    async adList(id){
      let params = {
        position:4,
        type:2
      }
      let [err,res] = await $await(playbackAPI.adList(params))
      if(res){
        this.setData({adList:res.banner})
      }
    },
    bindtapBanner(e){
      let { url , jump_type } = e.currentTarget.dataset;
      if ( jump_type == 1 ) {
        wx.navigateTo({
          url: `/pages/webview/webview?webURL=${encodeURIComponent(url)}`
        });
      }
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo')
    // console.log(userInfo)
    if (userInfo && userInfo.mobile) {
      this.getData()
      this.adList()
    }else if(userInfo && !userInfo.mobile){
      wx.reLaunch({
        url: '/pages/forgetPassword/forgetPassword',
      })
    }else{
      wx.reLaunch({
        url: '/pages/login/login?isBack=-1',
      })
    }
    // if(!userInfo){
    //   wx.reLaunch({
    //     url: '/pages/login/login',
    //   })
    // }else if(!userInfo.mobile){
    //   wx.reLaunch({
    //     url: '/pages/forgetPassword/forgetPassword',
    //   })
    // }else{
    //   this.getData()
    // }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

	  wx.hideHomeButton()
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})